fn main() {
    println!("Hello, world!");


    let v1 = vec![11, 22, 33];

    let mut v1_iter = v1.iter();


    let res = v1_iter.next().unwrap();


    println!("res is {:?}", res);

    let res = v1_iter.next();


    println!("res is {:?}", res);


    let res = v1_iter.next();


    println!("res is {:?}", res);


    let res = v1_iter.next();


    println!("res is {:?}", res);


    //  消耗迭代器的方法


    // 迭代器适配器


    let v1 = vec![11, 22, 33];

    let v2: Vec<_> = v1.iter().map(|x| x * 2).collect();


    println!("v2 : {:?}", v2);


    //   迭代器 + 闭包捕获环境变量


    let v1 = vec![11, 22, 33];


    let y = 20;
    let num: Vec<_> = v1.iter().filter(|x| **x == y).collect();

    println!("num is {:?}", num);
}


// 自定义迭代器  ， 主要是实现next方法


struct Counter {
    count: u32,
}


impl Counter {
    fn new() -> Counter {
        Counter { count: 0 }
    }
}


impl Iterator for Counter {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count < 5 {
            self.count += 1;
            Some(self.count)
        } else {
            None
        }
    }
}


#[test]
fn test_Iterator() {
    let mut counter = Counter::new();

    let a1 = counter.next();


    println!("a1 is {:?}", a1);
}



